package com.dhcc.sdc.cdss.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhcc.cdc.busi.entity.CdcEpisodeEntity;
import com.dhcc.cdc.busi.service.ICdcEpisodeService;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.sdc.cdss.dto.ArgumentData;
import com.dhcc.sdc.cdss.dto.GroupInfo;
import com.dhcc.sdc.cdss.service.ICdssApiService;

import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.service.ISdsQcentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("${sys.apiPath}")
public class CdssApiController extends BaseController {
    private String PREFIX = "/backend/sdc/manage/";

    @Autowired
    private ICdssApiService cdssApiService;
    @Autowired
    private ICdcEpisodeService cdcEpisodeService;
    @Autowired
    private ISdsQcentityService sdsQcentityService;
    @Autowired
    private ISdsCommonService sdsCommonService;

    /**
     * 跳转到生成cdss病种推荐数据页面
     */
    @RequestMapping("/initCdssRecommendData")
    public String initPortalData(Model model) {
        JSONArray entityMap = cdssApiService.getEntityMap();
        model.addAttribute("entityMap",entityMap);
        return PREFIX + "initCdssRecommendData.html";
    }

    /**
     * 跳转到生成cdss病种推荐数据日志记录页面
     */
    @RequestMapping("/initCdssRecommendData/log")
    public String CdssRecommendDataLog(@RequestParam Long qcEntity, Model model) {
        //获取病种
        JSONArray entityMap = sdsQcentityService.getUpEntityMap();
        JSONObject recommendTypeMap = sdsCommonService.getDicMap("CDSSRecommendType");
        model.addAttribute("entityId", qcEntity);
        model.addAttribute("entityMap", entityMap);
        model.addAttribute("recommendTypeMap", recommendTypeMap);
        return PREFIX + "initCdssRecommendDataLog.html";
    }

    /**
     * @param params
     * @return
     * @Description: 初始化患者单病种相关信息,全量数据推送给cdss，接收返回数据
     * @Title: initPatientData
     * @author: sunyingjie
     * @date: 2022年06月13日15:08:52
     */
    @RequestMapping(value = "/init")
    @ResponseBody
    public Object initPatientData(@RequestParam Map<String, Object> params) {
        String episodeId = params.get("patientId").toString();
        // 查询数据中心的对应的就诊id
        Map param = new HashMap();
        param.put("xcode", episodeId);
        List<CdcEpisodeEntity> cdcEpisods = cdcEpisodeService.list(param);
        if (cdcEpisods.size() == 0) {
            return R.error(204,"暂无该患者数据信息！");
        }
        episodeId = String.valueOf(cdcEpisods.get(0).getId());
        // 获取患者全量信息
        ArgumentData argumentData = cdssApiService.createArgumentData(episodeId);
        GroupInfo groupInfo = new GroupInfo();
        groupInfo.setName("SDS");
        argumentData.setGroupInfo(groupInfo);
        // 调用cdss接口，返回请求类型数据
        JSONArray cdssResult = new JSONArray();
        /*// 检验检查推荐
        argumentData.setType(ArgumentData.ParamType.RecomLabExam.toString());
        cdssResult.addAll(cdssApiService.cdssData(argumentData));
        // 诊疗推荐
        argumentData.setType(ArgumentData.ParamType.AssistTreat.toString());
        cdssResult.addAll(cdssApiService.cdssData(argumentData));
        // 护理推荐
        argumentData.setType(ArgumentData.ParamType.RecomNursing.toString());
        cdssResult.addAll(cdssApiService.cdssData(argumentData));
        // 评估量表推荐
        argumentData.setType(ArgumentData.ParamType.RecomAssess.toString());
        cdssResult.addAll(cdssApiService.cdssData(argumentData));*/
        // 一次请求全部数据
        String typeStr = ArgumentData.ParamType.RecommendLabExam + "," + ArgumentData.ParamType.AssistTreat + "," + ArgumentData.ParamType.RecommendNursing + "," + ArgumentData.ParamType.RecommendAssess;
        argumentData.setType(typeStr);
        cdssResult.addAll(cdssApiService.cdssData(argumentData).getJSONArray("cdssInfo"));
        Map result = new HashMap();
        result.put("result", cdssResult);
        return R.ok(result);
    }


    /**
     * @return
     * @Description: 更新病种推荐护理处置、推荐检验检查、推荐治疗方案、推荐评估量表、鉴别诊断
     * @Title: updateEntityRecommendData
     * @author: zhangdc
     * @date: 2022年06月24日15:08:52
     */
    @RequestMapping(value = "/updateEntityRecommendData")
    @ResponseBody
    public Object updateEntityRecommendData(@RequestParam Long entityId) throws Exception{
        // 调用cdss接口，返回请求类型数据
        User user = ShiroKit.getUser();
        String userName = user.getLoginName();
        Map<String, Object> result = cdssApiService.updateEntityRecommendData(entityId);
        return R.ok(result);
    }

}
